package com.changfeng.service.impl;

import com.changfeng.bean.ReatailersCommodity;
import com.changfeng.bean.dto.vo.CommodityConfirmVO;
import com.changfeng.bean.dto.vo.CommodityDetailsVO;
import com.changfeng.bean.dto.vo.CommodityListVO;
import com.changfeng.bean.dto.vo.CommodityQueryVO;
import com.changfeng.dao.ReatailersCommodityMapper;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private ReatailersCommodityMapper reatailersCommodityMapper;

    @Override
    public void saveCommodity(CommodityDetailsVO vo) {
        ReatailersCommodity reatailersCommodity = null;
        if (vo.getCommodityId() == null) {
            reatailersCommodity = new ReatailersCommodity();
            reatailersCommodity.setBrandId(vo.getBrandId());
            reatailersCommodity.setCategoryId(vo.getCategoryId());
            reatailersCommodity.setCommodityImgurl(vo.getCommodityImgurl());
            reatailersCommodity.setCommodityIntroduce(vo.getCommodityIntroduce());
            reatailersCommodity.setCommodityName(vo.getCommodityName());
            reatailersCommodity.setCommodityNum(vo.getCommodityNum());
            reatailersCommodity.setCommodityPrice(new BigDecimal(vo.getCommodityPrice()));
            reatailersCommodity.setGmtCreate(new Date());
            reatailersCommodity.setGmtModified(new Date());
            reatailersCommodity.setCommodityCollection(0);
            reatailersCommodityMapper.insert(reatailersCommodity);
        } else {
            reatailersCommodity =reatailersCommodityMapper.selectByPrimaryKey(vo.getCommodityId());
            if (reatailersCommodity == null) {
                throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
            }
            reatailersCommodity.setBrandId(vo.getBrandId());
            reatailersCommodity.setCategoryId(vo.getCategoryId());
            reatailersCommodity.setCommodityImgurl(vo.getCommodityImgurl());
            reatailersCommodity.setCommodityIntroduce(vo.getCommodityIntroduce());
            reatailersCommodity.setCommodityName(vo.getCommodityName());
            reatailersCommodity.setCommodityNum(vo.getCommodityNum());
            reatailersCommodity.setCommodityPrice(new BigDecimal(vo.getCommodityPrice()));
            reatailersCommodity.setGmtModified(new Date());
            reatailersCommodityMapper.updateByPrimaryKey(reatailersCommodity);
        }

    }

    @Override
    public CommodityDetailsVO getCommodityDetails(Integer commodityId) {
        ReatailersCommodity reatailersCommodity = reatailersCommodityMapper.selectByPrimaryKey(commodityId);
        if (reatailersCommodity == null) {
            throw new ChangFengException(ChangFengExceptionEnum.ERROR);
        }
        CommodityDetailsVO detailsVO = new CommodityDetailsVO();
        detailsVO.setBrandId(reatailersCommodity.getBrandId());
        detailsVO.setCategoryId(reatailersCommodity.getCategoryId());
        detailsVO.setCommodityId(reatailersCommodity.getCommodityId());
        detailsVO.setCommodityImgurl(reatailersCommodity.getCommodityImgurl());
        detailsVO.setCommodityIntroduce(reatailersCommodity.getCommodityIntroduce());
        detailsVO.setCommodityName(reatailersCommodity.getCommodityName());
        detailsVO.setCommodityNum(reatailersCommodity.getCommodityNum());
        detailsVO.setCommodityPrice(reatailersCommodity.getCommodityPrice().doubleValue());
        return detailsVO;
    }

    @Override
    public List<CommodityListVO> findCommodityList(CommodityQueryVO queryVO) {
        return reatailersCommodityMapper.findByQuery(queryVO);
    }

    @Override
    public void deleteCommodityById(Integer commodityId) {

        reatailersCommodityMapper.deleteByPrimaryKey(commodityId);
    }

    @Override
    public List<CommodityConfirmVO> findByIds(List<Integer> ids) {
        return reatailersCommodityMapper.selectByIds(ids);
    }
}
